using System;
using System.Collections.Generic;
using System.Text;

namespace Mud.Common.Physical
{
    /// <summary>
    /// Contains some physical helper methods.
    /// </summary>
    public static class PhysicalUtil
    {
        /// <summary>
        /// Calculates a direction which points from center to target.
        /// </summary>
        /// <param name="center">Center location.</param>
        /// <param name="target">Target location.</param>
        /// <returns>Orientation</returns>
        public static Orientation CalculateDirection(Location center, Location target)
        {
            if (center == target)
                throw new InvalidOperationException("Can not calculate direction when the center and target are the same point.");

            if (target.X == center.X && target.Y < center.Y)
                return Orientation.North;

            if (target.X < center.X && target.Y < center.Y)
                return Orientation.NorthWest;

            if (target.X < center.X && target.Y == center.Y)
                return Orientation.West;

            if (target.X < center.X && target.Y > center.Y)
                return Orientation.SouthWest;

            if (target.X == center.X && target.Y > center.Y)
                return Orientation.South;

            if (target.X > center.X && target.Y > center.Y)
                return Orientation.SouthEast;

            if (target.X > center.X && target.Y == center.Y)
                return Orientation.East;

            if (target.X > center.X && target.Y < center.Y)
                return Orientation.NorthEast;

            throw new InvalidOperationException("Can not calculate direction.");

        }

        public static bool IsInRange(Location source, Location target, int range)
        {
            int distanceSquared = (Math.Abs(source.X) - Math.Abs(target.X)) * (Math.Abs(source.X) - Math.Abs(target.X)) +
                (Math.Abs(source.Y) - Math.Abs(target.Y)) * (Math.Abs(source.Y) - Math.Abs(target.Y));
            return (distanceSquared < (range * range));

        }
    }
}
